//
//  RYChatVideoCell.m
//  SanYiTeaching
//
//  Created by ChenHao on 2019/8/1.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "RYChatVideoCell.h"
#import "SimpleMessage.h"

@interface RYChatVideoCell ()

-(void)initialize;

@end

@implementation RYChatVideoCell

-(NSDictionary *)attributeDictionary{
    if (self.messageDirection == MessageDirection_SEND) {
        return @{
                 @(NSTextCheckingTypeLink) : @{NSForegroundColorAttributeName : [UIColor blueColor]},
                 @(NSTextCheckingTypePhoneNumber) : @{NSForegroundColorAttributeName : [UIColor blueColor]}
                 };
    } else {
        return @{
                 @(NSTextCheckingTypeLink) : @{NSForegroundColorAttributeName : [UIColor blueColor]},
                 @(NSTextCheckingTypePhoneNumber) : @{NSForegroundColorAttributeName : [UIColor blueColor]}
                 };
    }
    return nil;
}
-(NSDictionary *)highlightedAttributeDictionary{
    return [self attributeDictionary];
    
}
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self initialize];
        
    }
    return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initialize];
    }
    return self;
}
-(void)initialize{
    
    self.textLabel = [[RCAttributedLabel alloc]init];
    self.textLabel.center = CGPointMake(self.messageTimeLabel.center.x+365, self.baseContentView.center.y+50);
    self.textLabel.bounds = CGRectMake(0, 0, 400, 50);
    self.textLabel.attributeDictionary = [self attributeDictionary];
    self.textLabel.highlightedAttributeDictionary = [self highlightedAttributeDictionary];
    [self.textLabel setFont:[UIFont systemFontOfSize:Text_Message_Font_Size]];
    self.textLabel.numberOfLines = 0;
    [self.textLabel setLineBreakMode:NSLineBreakByWordWrapping];
    [self.textLabel setTextAlignment:NSTextAlignmentCenter];
    [self.textLabel setTextColor:[UIColor blackColor]];
    self.textLabel.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1.0];
    UIButton *btn = [[UIButton alloc]initWithFrame:self.textLabel.frame];
    [btn addTarget:self action:@selector(clickLookInfoBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.baseContentView addSubview:btn];
    [self.baseContentView addSubview:self.textLabel];
}
+(CGSize)sizeForMessageModel:(RCMessageModel *)model withCollectionViewWidth:(CGFloat)collectionViewWidth referenceExtraHeight:(CGFloat)extraHeight{
    return     CGSizeMake(400, 80);
    
}
//当应用自定义消息时，必须实现该方法来返回cell的Size
-(void)setDataModel:(RCMessageModel *)model{
    [super setDataModel:model];
    
    [self setAutoLayout];
}
-(void)setAutoLayout{
    //    RCTextMessage *_textMessage = (RCTextMessage *)self.model.content;
    //    if (_textMessage) {
    //    } else {
    //        //DebugLog(@”[RongIMKit]: RCMessageModel.content is NOT RCTextMessage object”);
    //    }
    //    // ios 7
    //    CGSize __textSize =
    //    [_textMessage.content
    //     boundingRectWithSize:CGSizeMake(self.baseContentView.bounds.size.width -
    //                                     (10 + [RCIM sharedRCIM].globalMessagePortraitSize.width + 10) * 2 - 5 -
    //                                     35,
    //                                     MAXFLOAT)
    //     options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin |
    //     NSStringDrawingUsesFontLeading
    //     attributes:@{
    //                  NSFontAttributeName : [UIFont systemFontOfSize:Text_Message_Font_Size]
    //                  } context:nil]
    //    .size;
    //    __textSize = CGSizeMake(ceilf(__textSize.width), ceilf(__textSize.height));
    //    CGSize __labelSize = CGSizeMake(__textSize.width + 5, __textSize.height + 5);
    //
    //    CGFloat __bubbleWidth = __labelSize.width + 15 + 20 < 50 ? 50 : (__labelSize.width + 15 + 20);
    //    CGFloat __bubbleHeight = __labelSize.height + 5 + 5 < 35 ? 35 : (__labelSize.height + 5 + 5);
    //
    //    CGSize __bubbleSize = CGSizeMake(__bubbleWidth, __bubbleHeight);
    //
    //
    //    if (MessageDirection_RECEIVE == self.messageDirection) {
    //
    //        self.bubbleBackgroundView.frame = CGRectMake(0, 0, __bubbleSize.width, __bubbleSize.height);
    //
    //        self.textLabel.frame = CGRectMake(20, 5, __labelSize.width, __labelSize.height);
    ////        self.bubbleBackgroundView.image = [self imageNamed:@"chat_from_bg_normal" ofBundle:@"RongCloud.bundle"];
    ////        self.bubbleBackgroundView.image = [UIImage imageNamed:@""];
    //
    //        UIImage *image = self.bubbleBackgroundView.image;
    //        self.bubbleBackgroundView.image = [self.bubbleBackgroundView.image
    //                                           resizableImageWithCapInsets:UIEdgeInsetsMake(image.size.height * 0.8, image.size.width * 0.8,
    //                                                                                        image.size.height * 0.2, image.size.width * 0.2)];
    //    } else {
    //
    //
    //        self.bubbleBackgroundView.frame = CGRectMake(0, 0, __bubbleSize.width, __bubbleSize.height);
    //
    //        self.textLabel.frame = CGRectMake(15, 5, __labelSize.width, __labelSize.height);
    //
    ////        self.bubbleBackgroundView.image = [self imageNamed:@"chat_to_bg_normal" ofBundle:@"RongCloud.bundle"];
    ////        self.bubbleBackgroundView.image = [UIImage imageNamed:@"chat_message"];
    //        UIImage *image = self.bubbleBackgroundView.image;
    //        self.bubbleBackgroundView.image = [self.bubbleBackgroundView.image
    //                                           resizableImageWithCapInsets:UIEdgeInsetsMake(image.size.height * 0.8, image.size.width * 0.2,
    //                                                                                        image.size.height * 0.2, image.size.width * 0.8)];
    //    }
    if ([self.model.content isMemberOfClass:[SimpleMessage class]]){
        NSString *nameStr = self.model.userInfo.name;
        SimpleMessage *_textMessage = (SimpleMessage *)self.model.content;
        NSString *contenStr = _textMessage.content;
        NSString *str;
        if ([contenStr containsString:@":"]) {
            
            NSArray *arr = [contenStr componentsSeparatedByString:@":"];
            str = [NSString stringWithFormat:@"本次辅导有学生%@反馈为%@   查看",arr[1],arr[0]];
        }else{
            
            str = [NSString stringWithFormat:@"本次辅导有学生反馈为%@   查看",contenStr];
        }
//        NSString *str = [NSString stringWithFormat:@"本次辅导有学生%@反馈为%@   查看",arr[1],arr[0]];
        NSMutableAttributedString *contentStr = [[NSMutableAttributedString alloc]initWithString:str];
        //找出特定字符在整个字符串中的位置
        NSRange redRange = NSMakeRange([[contentStr string] rangeOfString:@"查看"].location, [[contentStr string] rangeOfString:@"查看"].length);
        //修改特定字符的颜色
        [contentStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#20B097"] range:redRange];
        //修改特定字符的字体大小
        [contentStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:14] range:redRange];
        [self.textLabel setAttributedText:contentStr];
        
    }
}

-(void)longPressed:(id)sender{
    UILongPressGestureRecognizer *press = (UILongPressGestureRecognizer *)sender;
    if (press.state == UIGestureRecognizerStateEnded) {
        //DebugLog(@”long press end”);
        return;
    } else if (press.state == UIGestureRecognizerStateBegan) {
        [self.delegate didLongTouchMessageCell:self.model inView:self.bubbleBackgroundView];
    }
}
-(void)clickLookInfoBtn{
    if (self.lookInfoBtnClick) {
        self.lookInfoBtnClick();
    }
}

@end
